package com.kuaimacode.kframework.wechat.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaimacode.kframework.mybatis.mapper.user.UserMapper;
import com.kuaimacode.kframework.mybatis.models.user.User;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class UserService extends ServiceImpl<UserMapper,User> {

    public User selectByName(String username) {
        
        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("username", username);
        
        return baseMapper.selectOne(qryWrapper);
    }

    public User selectByPhone(String phone) {
        
        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("phone", phone);
        return baseMapper.selectOne(qryWrapper);
    }

    public User selectByEmail(String email) {
        
        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("email", email);
        return baseMapper.selectOne(qryWrapper);
    }

    public User selectByOpenId(String openId) {
        
        QueryWrapper<User> qryWrapper = new QueryWrapper<>();
        qryWrapper.eq("open_id", openId);
        return baseMapper.selectOne(qryWrapper);
    }

    @Transactional
    public boolean add(User user) {
        //user.setPassword(crypt);
        return baseMapper.insert(user) > 0;
    }

    @Transactional
    public boolean modifyById(User user) {
        //User original = baseMapper.getById(user);
        return baseMapper.updateById(user) > 0;
    }
}